package com.njust.qrscan.camera;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.njust.qrscan.R;

import java.io.IOException;

/**
 * Created by XinYu on 2017/3/19.
 */

public class CustomCamera extends Activity{

    private Camera mCamera;
    private SurfaceView mPreview;
    private SurfaceHolder mHolder;
    private ImageView scanLine;
    private AutoFocusManager autoFocusManager;
    private Camera.Parameters parameters;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);
        mPreview= (SurfaceView) findViewById(R.id.surfaceView1);
        scanLine= (ImageView) findViewById(R.id.scan_line);

        mHolder=mPreview.getHolder();
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mHolder.setKeepScreenOn(true);
        mPreview.setFocusable(true);
       // mHolder.addCallback(this);
        mHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (mCamera==null)
                {
                    mCamera=Camera.open();
                    try {
                        mCamera.setPreviewDisplay(mHolder);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                mCamera.autoFocus(new Camera.AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                        if (success)
                        {
                            initCamera();
                            camera.cancelAutoFocus();
                        }
                    }
                });
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                releaseCamera();
            }
        });
    }

    private void setAnimation()
    {
        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.9f);
        animation.setDuration(4500);
        animation.setRepeatCount(-1);
        animation.setRepeatMode(Animation.RESTART);
        scanLine.startAnimation(animation);
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        if(mCamera==null)
//        {
//            mCamera=getCamera();     //绑定camera生命周期与activity生命周期绑定
//            if (mHolder!=null)
//            {
//                //setStartPreview(mCamera,mHolder);
//            }
//        }
//    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();
    }

    /**
     * 获取相机对象
     * @return
     */
    private Camera getCamera()
    {
        Camera camera;
        try {
            camera=Camera.open();
           initCamera();
            mCamera.startPreview();
        } catch (Exception e) {
            camera=null;
            e.printStackTrace();
        }
        return camera;
    }

    /**
     * 初始化相机信息
     */
    private void initCamera(){
        parameters=mCamera.getParameters();
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        //mCamera.setPreviewDisplay(mHolder);
        mCamera.setDisplayOrientation(90);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
        mCamera.cancelAutoFocus();

    }
    /**
     * 开始相机预览
     */
//    private void setStartPreview(Camera camera,SurfaceHolder holder)
//    {         //绑定camera对象和surfaceview
//        try {
//            initCamera();
////            camera.setPreviewDisplay(holder);
////            camera.setDisplayOrientation(90);
//            //camera.startPreview();  //系统默认预览为横屏，应调整预览角度
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//    }
    /**
     * 释放相机资源
     */
    private void releaseCamera()
    {
        if(autoFocusManager!=null)
        {
          //  mCamera.cancelAutoFocus();
            autoFocusManager=null;
        }
        if (mCamera!=null) {                    //回调至为空，取消绑定，再释放资源
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera=null;
        }
    }

//    @Override
//    public void surfaceCreated(SurfaceHolder holder) {
//       // setStartPreview(mCamera,mHolder);
//
//
//    }
//
//    @Override
//    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//        mCamera.stopPreview();
//       // setStartPreview(mCamera,mHolder);
//        mCamera.autoFocus(new Camera.AutoFocusCallback() {
//            @Override
//            public void onAutoFocus(boolean success, Camera camera) {
//                if (success)
//                {
//                    try {
//                        initCamera();
//                        mCamera.cancelAutoFocus();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//    }
//
//    @Override
//    public void surfaceDestroyed(SurfaceHolder holder) {
//        releaseCamera();
//    }
//
}
